<template>
  <div>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="链接名称" prop="linkName">
        <el-input v-model="form.linkName"></el-input>
      </el-form-item>
      <el-form-item label="链接地址" prop="linkUrl">
        <el-input
          placeholder="请输入链接地址"
          v-model="form.linkUrl"
          class="link-url"
        >
          <template slot="prepend">
            <el-select v-model="urlPre" placeholder="请选择">
              <el-option label="http://" value="http://"> </el-option>
              <el-option label="https://" value="https://"> </el-option>
            </el-select>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="toUpdate">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { get, toUpdate } from "@/api/link";

export default {
  props: {
    // 父组件传递的linkId
    activeId: {
      type: String,
      default: null,
    },
  },
  watch: {
    // 当id改变时，加载数据
    activeId: {
      // 饥饿监听  第一次赋值也需要调用该方法
      immediate: true,
      handler: function (newVal, oldVal) {
        this.getById();
      },
    },
  },
  data() {
    return {
      // 表单对象
      form: {},
      urlPre: "http://",
    };
  },
  methods: {
    getById() {
      get(this.activeId).then((res) => {
        if (res.data.linkUrl) {
          const url = res.data.linkUrl.split("//");
          this.urlPre = url[0] + "//";
          res.data.linkUrl = url[1];
        }
        this.form = res.data;
      });
    },
    toUpdate() {
      const temp = { ...this.form };
      console.log(this.urlPre+"--"+temp.linkUrl)
      temp.linkUrl = this.urlPre + temp.linkUrl;
      toUpdate(temp).then((res) => {
        this.$message.success(res.msg);
        this.$emit("after");
        this.$emit("closeDialog");
      });
    },
    closeDialog() {
      this.$emit("closeDialog");
    },
  },
};
</script>

<style scoped>
/*
">>>":  深度选择器
*/
.link-url >>> .el-input-group__prepend {
  width: 100px;
}
</style>